package com.ykt.vo;

import com.jfinal.kit.JsonKit;
import com.ykt.common.utils.page.PageData;

import java.util.Map;

/**
 * 返回结果值包装类
 *
 * @author long.hua
 * @version 1.0.0
 * @since 2015-12-08 23:01
 */
public class ResultVo<V> {

    private V data; // 返回的VO对象
    private int code;
    private PageVo page; // 分页信息
    private String message;


    public ResultVo() {
    }

    public ResultVo(int code, String message){
        this.code = code;
        this.message = message;
    }

    public ResultVo(V v, int code, String message){
        this.data = v;
        this.code = code;
        this.message = message;
    }

    public ResultVo(PageVo page, V v, int code, String message){
        this.page = page;
        this.data = v;
        this.code = code;
        this.message = message;
    }

    public static ResultVo success(){
        return success("ok!");
    }

    public static <V> ResultVo success(V data){
        if(data == null){
            return new ResultVo<V>(null, ResultStatus.SUCCESS, ResultStatus.SUCCESS_MSG);
        } else if(!(data instanceof PageData)){
            return new ResultVo<V>(data, ResultStatus.SUCCESS, ResultStatus.SUCCESS_MSG);
        } else {
            PageData pd = (PageData) data;
            return success(new PageVo(pd.getPageNo(), pd.getPageSize(), pd.getDataTotal()), pd.getPageData());
        }
    }

    public static <V> ResultVo success(PageVo page, V data){
        return new ResultVo<>(page, data, ResultStatus.SUCCESS, ResultStatus.SUCCESS_MSG);
    }

    public static <V> ResultVo<V> failure(){
        return new ResultVo<>(null, ResultStatus.FAILURE, ResultStatus.FAILURE_MSG);
    }

    public static <V> ResultVo<V> failure(V data){
        return new ResultVo<>(data, ResultStatus.FAILURE, ResultStatus.FAILURE_MSG);
    }

    public static <V> ResultVo<V> failure(V data, String msg){
        return new ResultVo<>(data, ResultStatus.FAILURE, msg);
    }

    public static <V> ResultVo<V> failure(V data, Map<String, String> msgMap){
        return new ResultVo<>(data, ResultStatus.FAILURE, toMsg(msgMap));
    }

    private static String toMsg(Map<String, String> msgMap){
        if(msgMap == null || msgMap.isEmpty()){
            return "{}";
        }

        return JsonKit.toJson(msgMap);
    }

    public V getData() {
        return data;
    }

    public void setData(V data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setMessage(Map<String, String> msgMap){
        this.message = toMsg(msgMap);
    }

    public PageVo getPage() {
        return page;
    }

    public void setPage(PageVo page) {
        this.page = page;
    }

    @Override
    public String toString() {
        return JsonKit.toJson(this);
    }

    public static class PageVo{

        private int pageNo;
        private int pageSize;
        private int total;


        public PageVo() {
			super();
		}

		public PageVo(int pageNo, int pageSize, int total) {
			super();
			this.pageNo = pageNo;
			this.pageSize = pageSize;
			this.total = total;
		}

		public int getPageNo() {
            return pageNo;
        }

        public void setPageNo(int pageNo) {
            this.pageNo = pageNo;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getTotal() {
            return total;
        }

        public void setTotal(int total) {
            this.total = total;
        }

    }

}

